package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strings"
)

type MajorCategory struct {
	Ctx iris.Context
}

type CreateMajorCategory struct {
	Id           string `json:"id"`
	CategoryType int    `json:"categoryType"`
	ParentId     int64  `json:"parentId"`
	Name         string `json:"name"`
}

type MajorCategoryListParam struct {
	CurPage int `json:"curPage"`
	PerPage int `json:"perPage"`
}

func (c *MajorCategory) OptionsFirst() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostFirst() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	majorCategoryList := make([]models.MajorCategory, 0)
	err = db.Where("parent_id = ?", 0).Find(&majorCategoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取专业一级分类列表失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取专业一级分类列表", majorCategoryList)
}

func (c *MajorCategory) OptionsSecond() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostSecond() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	majorCategoryList := make([]models.MajorCategory, 0)
	err = db.Where("parent_id > ?", 0).Find(&majorCategoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取专业二级分类列表失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取专业二级分类列表", majorCategoryList)
}

func (c *MajorCategory) OptionsCreate() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostCreate() interface{} {
	createMajorCategory := CreateMajorCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &createMajorCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错:%s", err.Error()), "")
	}

	if len(createMajorCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入专业分类名称", "")
	}
	if createMajorCategory.CategoryType <= 0 {
		return utils.CommonRes(1000, "请选择专业分类类型", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库连接出错：%s", err.Error()), "")
	}

	majorCategory := new(models.MajorCategory)
	majorCategory.CategoryType = createMajorCategory.CategoryType
	majorCategory.ParentId = createMajorCategory.ParentId
	majorCategory.Name = createMajorCategory.Name
	_, err = db.InsertOne(majorCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *MajorCategory) OptionsEdit() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostEdit() interface{} {
	createMajorCategory := CreateMajorCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &createMajorCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错：%s", err.Error()), "")
	}

	if len(createMajorCategory.Id) <= 0 {
		return utils.CommonRes(1000, "请提供专业分类id", "")
	}
	if len(createMajorCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入专业分类名称", "")
	}
	if createMajorCategory.CategoryType <= 0 {
		return utils.CommonRes(1000, "请选择专业分类类型", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库连接出错：%s", err.Error()), "")
	}

	data := map[string]interface{}{
		"name":          createMajorCategory.Name,
		"category_type": createMajorCategory.CategoryType,
		"parent_id":     createMajorCategory.ParentId,
	}
	_, err = db.Table(new(models.MajorCategory)).ID(createMajorCategory.Id).Update(data)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *MajorCategory) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostList() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	majorCategoryList := make([]models.MajorCategory, 0)
	err = db.Where("parent_id = ?", 0).Find(&majorCategoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取专业分类列表失败：%s", err.Error()), "")
	}

	for i, item := range majorCategoryList {
		majorCategoryList[i].CategoryTypeShow = models.MajorCategoryType[item.CategoryType]
		childList := make([]models.MajorCategory, 0)
		db.Where("parent_id = ?", item.Id).Find(&childList)
		for j, child := range childList {
			childList[j].CategoryTypeShow = models.MajorCategoryType[child.CategoryType]
		}
		majorCategoryList[i].Children = childList
	}

	return utils.CommonRes(20000, "成功获取专业分类列表", majorCategoryList)
}

func (c *MajorCategory) OptionsDetail() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "参数不完整", "")
	}
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	majorCategory := new(models.MajorCategory)
	ok, err := db.ID(editParam.Id).Get(majorCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", majorCategory)
}

func (c *MajorCategory) OptionsDel() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *MajorCategory) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错：%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "请提供要删除项的id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		majorCategory := new(models.MajorCategory)
		db.ID(id).Get(majorCategory)
		db.ID(id).Delete(majorCategory)
	}

	return utils.CommonRes(20000, "操作成功", "")
}
